package kr.co.doriclassic.summary_java.serviceUI;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import kr.co.doriclassic.summary_java.dao.CodeTitleDao;
import kr.co.doriclassic.summary_java.dao.InterfaceDao;
import kr.co.doriclassic.summary_java.dto.CodeTitle;

public class PanelCodeTitleTable extends JPanel implements ActionListener {
	private JTable table;
	private JLabel lblNewLabel;
	private CodeTitleDao dao;
	private JScrollPane scrollPane;
	private Object[][] arrs;
	private JPanel panel;
	private JTextField searchTf;
	private JButton btnSearch;
	private CodeTitle ct;
	private ArrayList<CodeTitle> list;
	
	public InterfaceDao<CodeTitle> getDao() {
		return dao;		
	}
	public void setDao(CodeTitleDao dao){
		this.dao = dao;	
		inputTable();
		tableCellAlignAndWidth();	
	}
	
	public String getLblNewLabel() {
		return lblNewLabel.getText();
	}

	public void setLblNewLabel(String s) {
		lblNewLabel.setText(s);
	}
	
	
	public ArrayList<CodeTitle> getList() {
		return list;
	}
	public void setList(ArrayList<CodeTitle> list) {
		this.list = list;
	}
	public PanelCodeTitleTable() {
		initialize();		
	}
		
	private void tableCellAlignAndWidth() {
		DefaultTableCellRenderer dtcrRight = new DefaultTableCellRenderer();
		dtcrRight.setHorizontalAlignment(SwingConstants.RIGHT);
		TableColumnModel cModel = table.getColumnModel();		
		int columnCount = cModel.getColumnCount();		
		cModel.getColumn(0).setPreferredWidth(30);
		cModel.getColumn(1).setPreferredWidth(50);
		
		for (int i = 0; i < columnCount; i++) {
			cModel.getColumn(i).setCellRenderer(dtcrRight);
		}		
	}	
	
	private void initialize() {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		lblNewLabel = new JLabel("New label");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		add(lblNewLabel);
		
		panel = new JPanel();
		add(panel);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		searchTf = new JTextField();
		panel.add(searchTf);
		searchTf.setColumns(10);
		
		btnSearch = new JButton("검색");
		btnSearch.addActionListener(this);
		panel.add(btnSearch);
		
		scrollPane = new JScrollPane();
		add(scrollPane);
		
		table = new JTable();
		
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				CodeTitle temp = new CodeTitle();
				int no = (int) table.getValueAt(table.getSelectedRow(),0);
				temp.setTitleNo(no);				
				try {
					ct = dao.searchItem(temp);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				removeAll();
				add(new PanelCodeTitleUI(ct));			
				revalidate();
			}
		});
			
		table.setAutoCreateRowSorter(true);//탭정렬			
		scrollPane.setViewportView(table);
	}
	
	
	
	private void inputTable() {
		try {
			arrs = new Object[list.size()][2];
			for(int i =0;i<list.size();i++){
					CodeTitle d = list.get(i);
					arrs[i][0]=d.getTitleNo();
					arrs[i][1]=d.getTitleName();						
			}
		}finally{
			String[] colNames = {"직급번호	","직급명"};
			TableModel model = new DefaultTableModel(arrs, colNames);
			table.setModel(model);			
		} 					
		revalidate();
	}	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnSearch) {
			try {						
				list = dao.searchList(searchTf.getText().trim());
				PanelCodeTitleTable ct = new PanelCodeTitleTable();
				ct.setLblNewLabel("부서정보");
				ct.setList(list);
				ct.setDao(dao);
				rPaint(ct);
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");				
			} catch (NullPointerException e1) {
				JOptionPane.showMessageDialog(this, "결과가 없습니다.");
				
			} 
		}				
		
	}
	private void rPaint(JPanel ui) {
		this.removeAll();
		this.add(ui);
		this.revalidate();		
	}
	
}
